<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="客户" prop="customerId">
              <el-select v-model="ruleForm.customerId" style="width: 100%" filterable>
                <el-option v-for="item in customerList" :key="item.id" :label="item.customerName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订货日期" prop="dateOfOrder">
              <el-date-picker v-model="ruleForm.dateOfOrder" type="datetime" placeholder="选择日期时间" style="width: 100%" format="yyyy-MM-dd" value-format="yyyy-MM-dd" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="订单号" prop="orderNo">
              <el-input v-model="ruleForm.orderNo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料" prop="materialId">
              <el-select v-model="ruleForm.materialId" filterable style="width: 100%" @change="ruleForm.unit = materialList.find(item => item.id === ruleForm.materialId).unit">
                <el-option v-for="item in materialList" :key="item.id" :label="`${item.materialName} ${item.unit ? '【' + item.unit + '】' : ''}`" :value="item.id">
                  <span style="float: left">{{ item.materialName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.unit }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="订货数量" prop="orderQuantity">
              <el-input-number
                @change="
                  value => {
                    ruleForm.amount = (value || 0) * (ruleForm.salePrice || 0)
                  }
                "
                v-model="ruleForm.orderQuantity"
                style="width: 100%"
                :precision="0"
                :min="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划单价" prop="plannedUnitPrice">
              <el-input-number
                @change="
                  value => {
                    ruleForm.salePrice = (value || 0) * (ruleForm.discountRate || 0)
                    ruleForm.amount = (ruleForm.orderQuantity || 0) * (ruleForm.salePrice || 0)
                  }
                "
                v-model="ruleForm.plannedUnitPrice"
                style="width: 100%"
                :precision="0"
                :min="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="折扣率" prop="discountRate">
              <el-input-number
                @change="
                  value => {
                    ruleForm.salePrice = (value || 0) * (ruleForm.plannedUnitPrice || 0)
                    ruleForm.amount = (ruleForm.salePrice || 0) * (ruleForm.orderQuantity || 0)
                  }
                "
                v-model="ruleForm.discountRate"
                style="width: 100%"
                :precision="2"
                :min="0"
                :max="100.0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售单价" prop="salePrice">
              <el-input-number
                @change="
                  value => {
                    ruleForm.amount = (value || 0) * (ruleForm.orderQuantity || 0)
                  }
                "
                v-model="ruleForm.salePrice"
                style="width: 100%"
                :precision="2"
                :min="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="金额" prop="amount">
              <el-input-number v-model="ruleForm.amount" disabled style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划数量" prop="plannedQuantity">
              <el-input-number
                @change="
                  value => {
                    ruleForm.amount = (value || 0) * (ruleForm.salePrice || 0)
                    ruleForm.finishRate = ((value || 0) - (ruleForm.surplusQuantity || 0)) / (value || 1)
                  }
                "
                v-model="ruleForm.plannedQuantity"
                style="width: 100%"
                :precision="0"
                :min="1"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="剩余数量" prop="surplusQuantity">
              <el-input-number
                @change="
                  value => {
                    ruleForm.finishRate = ((ruleForm.plannedQuantity || 0) - (value || 0)) / (ruleForm.plannedQuantity || 1)
                  }
                "
                v-model="ruleForm.surplusQuantity"
                style="width: 100%"
                :precision="0"
                :min="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成率" prop="finishRate">
              <el-input-number v-model="ruleForm.finishRate" disabled style="width: 100%" :precision="2" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单交期" prop="orderDeliveryTime">
              <el-date-picker v-model="ruleForm.orderDeliveryTime" type="datetime" placeholder="选择日期时间" style="width: 100%" format="yyyy-MM-dd" value-format="yyyy-MM-dd" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <!--     <span slot="footer">
      <el-button type="info" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </span> -->
  </el-dialog>
</template>
<script>
import { getCustomerListApi } from '@/api/public'
export default {
  props: {
    materialList: {
      type: Array,
      default: () => {
        return {}
      }
    },
    userAllList: {
      type: Array,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      title: '',
      ruleForm: {
        customerId: '',
        dateOfOrder: '',
        orderNo: '',
        materialId: '',
        orderQuantity: '',
        plannedUnitPrice: '',
        discountRate: '',
        salePrice: '',
        surplusQuantity: '',
        orderDeliveryTime: ''
      },
      wmsListAll: [],
      rules: {
        customerId: [{ required: true, message: '客户不能为空！', trigger: 'change' }],
        dateOfOrder: [{ required: false, message: '订货日期不能为空！', trigger: 'change' }],
        orderDeliveryTime: [{ required: false, message: '订单交期不能为空！', trigger: 'change' }],
        orderNo: [{ required: true, message: '订单号不能为空！', trigger: 'change' }],
        materialId: [{ required: true, message: '物料不能为空！', trigger: 'change' }],
        orderQuantity: [{ required: true, message: '订货数量不能为空！', trigger: 'change' }],
        plannedUnitPrice: [{ required: true, message: '计划单价不能为空！', trigger: 'change' }],
        discountRate: [{ required: true, message: '折扣率不能为空！', trigger: 'change' }],
        salePrice: [{ required: true, message: '销售单价不能为空！', trigger: 'change' }],
        surplusQuantity: [{ required: true, message: '剩余数量不能为空！', trigger: 'change' }]
      },
      dialogVisible: false,
      customerList: []
    }
  },
  watch: {},
  mounted() {
    this.getAllCustomers()
  },
  methods: {
    /**
     * @description: 获取所有客户
     * @return {*}
     */
    async getAllCustomers() {
      const res = await getCustomerListApi()
      if (res?.data.code === 200) {
        this.customerList = res.data.data
      }
    },
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
      console.log(this.ruleForm)
      this.dialogVisible = true
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const res = await this.$axios.post('/sale/statistics/rate/save', this.ruleForm)
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = Object.assign({}, this.$options.data.call(this).ruleForm)
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
